#-------------------------------------------------------------------------------
# target file processor
# params:
#   src_path dep_file dep_path dep_file
# eg:
#   src/jpl/ target.mak dep/jpl/ target.mak
#  (src/jpl/target.mak -> dep/jpl/target.mak)


# NB: The emitted make syntax now relies on the "sourcesubst" function (which is
#    in build.mak), to handle the the 'sr3' third-party source root existing
#    outside of 'src'. Previous versions of this file used a simple variable
#    substitution instead, eg $(var:a%=b%).

#-------------------------------------------------------------------------------
# params

# it's easier for make to split these up with $(dir) and $(notdir), than to try
# to split the path in here.

src_path=$1
src_target=$2
dep_path=$3
dep_target=$4

src=$src_path$src_target
dep=$dep_path$dep_target

#-------------------------------------------------------------------------------
# is this a BIN, DLL, LIB OR PDF target?

TYPE=`sed -n -e 's/\(BIN\)_TARGET.*/\1/p' $src`

if [ -z $TYPE ]; then
	TYPE=`sed -n -e's/\(DLL\)_TARGET.*/\1/p' $src`;
fi

if [ -z $TYPE ]; then
	TYPE=`sed -n -e's/\(LIB\)_TARGET.*/\1/p' $src`;
fi

if [ -z $TYPE ]; then
	TYPE=`sed -n -e's/\(PDF\)_TARGET.*/\1/p' $src`;
fi

case $TYPE in
	BIN)
		type="bin";;
	DLL)
		type="dll";;
	LIB)
		type="lib";;
	PDF)
		type="pdf";;
	*)
		echo > /dev/stderr;
		echo "$src: bad target type ${TYPE}_TARGET" > /dev/stderr;
		echo > /dev/stderr;
		exit 1;;
esac

#-------------------------------------------------------------------------------
# set up other vars

TARGET="$TYPE""_TARGET"
path="$type/"
eval_target="\$(call $type""_target,\$($TARGET))"

#-------------------------------------------------------------------------------
# output

echo "#this makefile is autogenerated from $src"
echo BIN_TARGET:=
echo DLL_TARGET:=
echo LIB_TARGET:=
echo PDF_TARGET:=
echo INCLUDE:=
echo SOURCE:=
echo "\$(PLATFORM)_SOURCE:="
echo DLLS:=
echo LIBS:=
echo XLIBS:=

# This is a bit of a hack to expose the current source path (and the autogen output
# path) to the target.mak fragments. This allows me to express ad-hoc dependencies
# when an autogen file depends on other files which we can't determine programatically)
echo _SRC_PATH:=$src_path
echo "SOURCE_PATH:=\$(_SRC_PATH:%/=%)"
echo "AUTOGEN_PATH:=\$(call sourcesubst,obj/%,\$(SOURCE_PATH))"
echo _SRC_PATH:=
echo "#================================================================================"
echo "# YOUR MAKEFILE HERE:"
echo

cat $src
echo

echo "#================================================================================"
echo "$TARGET""S+=$eval_target"
echo "$eval_target""_SOURCE:=\$(foreach source,\$(SOURCE) \$(\$(PLATFORM)_SOURCE),$src_path\$(source))"
echo "INCLUDES+=\$(INCLUDE)"
echo "$eval_target""_INC:=\$(LOCAL_INCLUDE)"
echo "$eval_target""_DLL:=\$(foreach dll,\$(DLLS),\$(call dll_target,\$(dll)))"
echo "$eval_target""_LIB:=\$(foreach lib,\$(LIBS),\$(call lib_target,\$(lib)))"
echo "$eval_target""_XLIB:=\$(XLIBS) \$(\$(PLATFORM)_XLIBS)"
echo "$eval_target""_OBJ:=\$(foreach source,\$($eval_target""_SOURCE),\$(call sourcesubst,obj/\$(TYPE)/%.o,\$(source)))"
echo "$eval_target""_DEP:=\$(foreach source,\$($eval_target""_SOURCE),\$(call sourcesubst,dep/\$(TYPE)/%.d,\$(source)))"
echo "$eval_target""_TARGETFILE:=$src"
echo "$eval_target""_DEPFILE:=$dep"

